package com.orhon.smartcampus.modules.notice.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.notice.dao.NoticeDao;
import com.orhon.smartcampus.modules.notice.entity.Notice;
import com.orhon.smartcampus.modules.notice.gqlModel.GQLNotice;
import com.orhon.smartcampus.modules.notice.mm.NoticeMapper;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class NoticesFetcher implements DataFetcher {

    @Autowired
    NoticeDao noticeDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
//        String token = tokenService.getSystemToken();
//        if(token==null) {
//            return  R.NoLogin();
//        }
//        Long schoolId = JwtUtils.getSchoolId(token);

        List<GQLNotice> GQLNotice = new ArrayList<>();


        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from Notice as notice where notice.deletedAt = null";
//        String hql = "from Notice as notice where deleted_at = null and school_id = " + schoolId;
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("id")!=null) {
                hql += "and notice.id" + Util.decide(map.get("id"));
            }
            if(map.get("school_id")!=null) {
                hql += "and notice.school_id " +Util.decide( map.get("school_id"));
            }
            if(map.get("noticeTitle")!=null) {
                hql += "and notice.noticeTitle " +Util.decide( map.get("noticeTitle"));
            }
            if(map.get("noticeContent")!=null) {
                hql += "and notice.noticeContent " +Util.decide( map.get("noticeContent"));
            }
            if(map.get("sendingTime")!=null) {
                hql += "and notice.sendingTime " +Util.decide( map.get("sendingTime"));
            }
            if(map.get("school_ids")!=null) {
                hql += "and notice.id IN (select notice_id from NoticeAndSchool where school_id "+Util.decide( map.get("school_ids"))+") ";
            }
        }
        hql += " ORDER BY notice.createdAt DESC";

        List<Notice> list = noticeDao.find(hql,null,page.getPage(),page.getLimit());
        System.out.println("通知公告总数:"+list.size());



        for (Notice item : list) {

            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("data/school")){
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }

            List<GQLSchool> gqlSchools = null;
            if (env.getSelectionSet().contains("data/schools")){
                List<Schools> schoolsList = item.getSchools();
                gqlSchools = SchoolMapper.MAPPER.toSchoolList(schoolsList);
                System.out.println("接受人:"+gqlSchools.toString());
            }

            GQLNotice notice = NoticeMapper.MAPPER.toNotice(item);
            notice.setSchool(gqlSchool);
            notice.setSchools(gqlSchools);

            GQLNotice.add(notice);
        }

        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
            total = noticeDao.getTotalCount(hql);
        }
        return Result.ok().put("data", GQLNotice).put("total", total);
    }
}
